package com.ruoyi.jiajia.controller;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 社工Controller
 *
 * @author ruoyi
 * @date 2021-10-13
 */
@Controller
@RequestMapping("/jiajia/worker")
public class JiajiaWorkerController extends BaseController {
    private String prefix = "jiajia/worker";

    @Autowired
    private IJiajiaWorkerService jiajiaWorkerService;

    @Autowired
    private IJiajiaWorkerPhotoService workerPhotoService;

    @Autowired
    private IJiajiaItemService ItemService;

    @Autowired
    private IJiajiaWorkerItemService workerItemService;

    @Autowired
    private IJiajiaWorkerRenzhengService workerRenzhengService;

    @Autowired
    private IJiajiaZiZhiService ziZhiService;

    @RequiresPermissions("jiajia:worker:view")
    @GetMapping()
    public String worker() {
        return prefix + "/worker";
    }

    /**
     * 查询社工列表
     */
    @RequiresPermissions("jiajia:worker:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaWorker jiajiaWorker) {
        startPage();
        List<JiajiaWorker> list = jiajiaWorkerService.selectJiajiaWorkerList(jiajiaWorker);
        return getDataTable(list);
    }


    /**
     * 导出社工列表
     */
    @RequiresPermissions("jiajia:worker:export")
    @Log(title = "社工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaWorker jiajiaWorker) {
        List<JiajiaWorker> list = jiajiaWorkerService.selectJiajiaWorkerList(jiajiaWorker);
        ExcelUtil<JiajiaWorker> util = new ExcelUtil<JiajiaWorker>(JiajiaWorker.class);
        return util.exportExcel(list, "社工数据");
    }

    /**
     * 新增社工
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存社工
     */
    @RequiresPermissions("jiajia:worker:add")
    @Log(title = "社工", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaWorker jiajiaWorker) {
        String regex = "^[1][3,4,5,6,7,8,9][0-9]{9}$";
        if (jiajiaWorker.getPhone()==null||jiajiaWorker.getPhone().length() != 11) {
            return AjaxResult.error("您输入的手机号有误请重新输入");
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(jiajiaWorker.getPhone());
            boolean isMatch = m.matches();
            if (!isMatch) {
                return AjaxResult.error("您输入的手机号有误请重新输入");
            }
        }
        jiajiaWorker.setAddtime(new Date());
//        AjaxResult toAjax =jiajiaWorkerService.insertJiajiaWorker(jiajiaWorker);
        int i = jiajiaWorkerService.insertJiajiaWorker(jiajiaWorker);
        if (i < 0){
            return AjaxResult.error("该用户名已存在");
        }
        return AjaxResult.success(i);
    }

    /**
     * 修改社工
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        JiajiaWorker jiajiaWorker = jiajiaWorkerService.selectJiajiaWorkerById(id);
        mmap.put("jiajiaWorker", jiajiaWorker);
        return prefix + "/edit";
    }

    /**
     * 修改保存社工
     */
    @RequiresPermissions("jiajia:worker:edit")
    @Log(title = "社工", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaWorker jiajiaWorker) {
        return toAjax(jiajiaWorkerService.updateJiajiaWorker(jiajiaWorker));
    }

    /**
     * 删除社工
     */
    @RequiresPermissions("jiajia:worker:remove")
    @Log(title = "社工", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(jiajiaWorkerService.deleteJiajiaWorkerByIds(ids));
    }


    /**
     * 查询详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        JiajiaWorker jiajiaWorker = jiajiaWorkerService.selectJiajiaWorkerById(id);
        mmap.put("info", jiajiaWorker);


        JiajiaWorkerPhoto workerPhoto = new JiajiaWorkerPhoto();
        workerPhoto.setWid(id);
        List<JiajiaWorkerPhoto> photoList = workerPhotoService.selectJiajiaWorkerPhotoList(workerPhoto);
        mmap.put("photoLists", photoList);

        JiajiaWorkerItem workerItem = new JiajiaWorkerItem();
        workerItem.setWid(id);
        List<JiajiaWorkerItem> workerItemList = workerItemService.selectJiajiaWorkerItemList(workerItem);
        mmap.put("workerItemList", workerItemList);

        mmap.put("id", id);
        JiajiaWorkerRenzheng workerRenzheng = workerRenzhengService.selectJiajiaWorkerRenzhengByWid(id);
        if(workerRenzheng == null){
            workerRenzheng = new JiajiaWorkerRenzheng();
        }
        mmap.put("WorkerRenzheng", workerRenzheng);

        JiajiaZiZhi ziZhi = new JiajiaZiZhi();
        ziZhi.setWid(id);
        List<JiajiaZiZhi> ziZhiList = ziZhiService.selectJiajiaZiZhiList(ziZhi);
        mmap.put("ziZhiList", ziZhiList);

        return prefix + "/detail";
    }


//    /**
//     * 新增社工
//     */
//    @GetMapping("/getInfo/{id}")
//    @ResponseBody
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        JiajiaWorker jiajiaWorker = jiajiaWorkerService.selectJiajiaWorkerById(id);
//
//        return AjaxResult.success("获取成功",jiajiaWorker);
//    }

}
